<template>
  <section v-loading="loading" class="cms-body">

    <div class="cms-list-header flex-between">
      <el-button type="primary" icon="el-icon-plus" @click="routerLink('/sysLive/save','save',0)" v-perms="'/sysLive/save'">添加</el-button>
      <!-- 右操作 -->
      <div>
        <el-select v-model="params.liveStatus" @change="goQuery()">
          <el-option label="状态" value=""></el-option>
          <el-option label="未开始" value="0"></el-option>
          <el-option label="已开始" value="1"></el-option>
          <el-option label="已结束" value="2"></el-option>
        </el-select>
        <cms-input label="直播标题" v-model="params.liveTitle"></cms-input>
        <cms-input label="创建用户" v-model="params.userName"></cms-input>
        <el-button @click="goQuery()">查询</el-button>
      </div>
    </div>

    <!-- 数据表格显示区域 -->
    <el-table :data="tableData" stripe style="width: 100%" @selection-change="checkIds">
      <el-table-column type="selection" width="35" align="center"></el-table-column>
      <el-table-column prop="id" label="ID" width="140" align="center"></el-table-column>
      <el-table-column prop="liveTitle" label="直播标题" align="center"></el-table-column>
      <el-table-column prop="userName" label="创建人" align="center"></el-table-column>
      <el-table-column prop="liveType" label="类型" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.liveType ==0">后台创建</span>
          <span v-if="scope.row.liveType == 1">用户创建</span>
        </template>
      </el-table-column>
      <el-table-column prop="liveStatus" label="直播状态" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.liveStatus ==0">未开始</span>
          <span v-if="scope.row.liveStatus == 1">已开始</span>
          <span v-if="scope.row.liveStatus == 2">已结束</span>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="上架状态" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.status ==0">待上架</span>
          <span v-if="scope.row.status == -1">已下架</span>
          <span v-if="scope.row.status == 2">已上架</span>
        </template>
      </el-table-column>
      <el-table-column prop="liveTime" label="直播时间(s)" align="center" width="80"></el-table-column>

      <el-table-column prop="liveUrl" label="直播地址" align="center" width="60">
        <template slot-scope="scope">
          <cms-button type="view" @click.native="lookUrl(scope.row.liveUrl,1)"></cms-button>
        </template>
      </el-table-column>

      <el-table-column prop="recordedUrl" label="回放地址" align="center" width="60">
        <template slot-scope="scope">
          <cms-button type="view" @click.native="lookUrl(scope.row.recordedUrl,2)"></cms-button>
        </template>
      </el-table-column>
      <el-table-column prop="watchNum" label="直播观看人数" align="center" width="100"></el-table-column>
      <el-table-column prop="reviewNum" label="回顾观看人数" align="center" width="100"></el-table-column>
      <el-table-column prop="createTime" label="创建日期" align="center" width="120"></el-table-column>

      <el-table-column width="80" label="操作" align="center">
        <div slot-scope="scope">
          <cms-button type="edit" @click.native="routerLink('/sysLive/update','update',scope.row.id)" v-perms="'/sysLive/update'"></cms-button>
          <cms-button type="delete" @click.native="deleteBatch($api.sysLiveDelete,scope.row.id)" v-perms="'/sysLive/delete'"></cms-button>
        </div>
      </el-table-column>
    </el-table>

    <!-- 表格底部 -->
    <div class="cms-list-footer">
      <div class="cms-left">
        <el-button :disabled="disabled" @click="deleteBatch($api.sysLiveDelete,ids)" v-perms="'/sysLive/delete'">批量删除
        </el-button>
        <el-button :disabled="disabled" @click="updateBatch($api.sysLiveChangeStatus,ids,0)"
                   v-perms="'/sysLive/changeStatus'">批量下架
        </el-button>
        <el-button :disabled="disabled" @click="updateBatch($api.sysLiveChangeStatus,ids,2)"
                   v-perms="'/sysLive/changeStatus'">批量上架
        </el-button>
      </div>
      <!-- cms分页组件 -->
      <cms-pagination :total="total" @change="getPages"></cms-pagination>
    </div>

    <el-dialog :title="dialogTitle" :visible.sync="dialogTableVisible">
      <el-table :data="dialogUrlData">
        <el-table-column prop="url" label="地址"></el-table-column>
        <el-table-column width="200" label="操作" align="center">
          <div slot-scope="scope">
            <a v-if="scope.row.url!=''" :href="scope.row.url" target="_blank" download="">下载</a>
          </div>
        </el-table-column>
      </el-table>
    </el-dialog>

  </section>
</template>
<script>
  import listMixins from '@/mixins/list'
  import axios from "axios/index";

  export default {
    mixins: [listMixins],
    data() {
      return {
        params: {
          pageNo: 1,
          pageSize: 10,
          liveStatus: '',
          liveTitle: '',
          userName: ''
        },
        dialogTableVisible: false,
        dialogTitle: "地址",
        dialogUrlData: []
      }
    },
    methods: {
      goQuery() {
        this.getTableData(this.params);
      },
      lookUrl: function (url, type) {
        this.dialogUrlData = [];
        if (type == 1) {
          this.dialogTitle = "直播地址";
        }
        if (type == 2) {
          this.dialogTitle = "回放地址";
        }
        let result = url.split(',');
        for (let i in result) {
          let url = result[i];
          let param = {url: url};
          this.dialogUrlData.push(param);
        }
        this.dialogTableVisible = true;

      },
      updateBatch(url, ids, status) {
        let tip = '';
        if (status == 1) {
          tip = '下架'
        }
        if (status == 2) {
          tip = '上架'
        }
        this.$confirm('是否' + tip + '？', '警告', {type: "error"}).then(mes => {
          this.$http.post(url, {ids: ids, status: status}).then(res => {
            console.log(res);
            if (res.code == "200") {
              this.successMessage(tip + '成功');
              this.getTableData(this.params);
            }
          });
        }).catch(error => {
          console.log(error);
        });
      }
    },
    created() {
      this.initTableData(this.$api.sysLivePageList, this.params);
    }
  };
</script>
